package com.example.algorithm.model;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Stack;
import java.util.StringJoiner;

/**
 * @author Administrator
 * @date 2024/7/25 19:33
 * @description
 */
@Slf4j
@Data
public class TreeNode {
    /**
     * 值
     */
   public int val;
    /**
     * 左子树
     */
    public TreeNode left;
    /**
     * 右子树
     */
    public TreeNode right;

    public  TreeNode(int val) { this.val = val; }

    public TreeNode(int val ,TreeNode left,TreeNode right){
        this.val = val;
        this.left=left;
        this.right =right;
    }


    public void addNode(TreeNode node){
        if(node==null){
            return;
        }
        if (node.val<this.val){
            if(this.left==null){
                this.left = node;
            }else{
                this.left.addNode(node);
            }
        }else{
            if(this.right==null){
                this.right = node;
            }else{
                this.right.addNode(node);
            }
        }
    }

    public  void print(){
        StringJoiner stringJoiner = new StringJoiner(",","[","]");
        Stack<TreeNode> stack = new Stack<>();
        TreeNode curr = this;
        while (!stack.isEmpty()||curr!=null){
            if(curr!=null){
                stack.push(curr);
                curr =curr.left;
            }else{
                curr = stack.pop();
                stringJoiner.add(String.valueOf(curr.val));
                curr = curr.right;
            }
        }
        log.info("结果：{}",stringJoiner);
    }


}
